home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / dis.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  247 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Disassembler of Python byte code into mnemonics.'''
  5. import sys
  6. import types
  7. from opcode import *
  8. from opcode import __all__ as _opcodes_all
  9. __all__ = [
  10.     'dis',
  11.     'disassemble',
  12.     'distb',
  13.     'disco',
  14.     'findlinestarts',
  15.     'findlabels'] + _opcodes_all
  16. del _opcodes_all
  17. _have_code = (types.MethodType, types.FunctionType, types.CodeType, types.ClassType, type)
  18.  
  19. def dis(x = None):
  20.     '''Disassemble classes, methods, functions, or code.
  21.  
  22.     With no argument, disassemble the last traceback.
  23.  
  24.     '''
  25.     if x is None:
  26.         distb()
  27.         return None
  28.     if None(x, types.InstanceType):
  29.         x = x.__class__
  30.     if hasattr(x, 'im_func'):
  31.         x = x.im_func
  32.     if hasattr(x, 'func_code'):
  33.         x = x.func_code
  34.     if hasattr(x, '__dict__'):
  35.         items = x.__dict__.items()
  36.         items.sort()
  37.         for name, x1 in items:
  38.             if isinstance(x1, _have_code):
  39.                 print 'Disassembly of %s:' % name
  40.                 
  41.                 try:
  42.                     dis(x1)
  43.                 except TypeError:
  44.                     msg = None
  45.                     print 'Sorry:', msg
  46.  
  47.                 print 
  48.                 continue
  49.     if hasattr(x, 'co_code'):
  50.         disassemble(x)
  51.     elif isinstance(x, str):
  52.         disassemble_string(x)
  53.     else:
  54.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  55.  
  56.  
  57. def distb(tb = None):
  58.     '''Disassemble a traceback (default: last traceback).'''
  59.     if tb is None:
  60.         
  61.         try:
  62.             tb = sys.last_traceback
  63.         except AttributeError:
  64.             raise RuntimeError, 'no last traceback to disassemble'
  65.  
  66.         while tb.tb_next:
  67.             tb = tb.tb_next
  68.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  69.  
  70.  
  71. def disassemble(co, lasti = -1):
  72.     '''Disassemble a code object.'''
  73.     code = co.co_code
  74.     labels = findlabels(code)
  75.     linestarts = dict(findlinestarts(co))
  76.     n = len(code)
  77.     i = 0
  78.     extended_arg = 0
  79.     free = None
  80.     while i < n:
  81.         c = code[i]
  82.         op = ord(c)
  83.         if i in linestarts:
  84.             if i > 0:
  85.                 print 
  86.             print '%3d' % linestarts[i],
  87.         else:
  88.             print '   ',
  89.         if i == lasti:
  90.             print '-->',
  91.         else:
  92.             print '   ',
  93.         if i in labels:
  94.             print '>>',
  95.         else:
  96.             print '  ',
  97.         print repr(i).rjust(4), opname[op].ljust(20),
  98.         i = i + 1
  99.         if op >= HAVE_ARGUMENT:
  100.             oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
  101.             extended_arg = 0
  102.             i = i + 2
  103.             if op == EXTENDED_ARG:
  104.                 extended_arg = oparg * 0x10000L
  105.             print repr(oparg).rjust(5),
  106.             if op in hasconst:
  107.                 print '(' + repr(co.co_consts[oparg]) + ')',
  108.             elif op in hasname:
  109.                 print '(' + co.co_names[oparg] + ')',
  110.             elif op in hasjrel:
  111.                 print '(to ' + repr(i + oparg) + ')',
  112.             elif op in haslocal:
  113.                 print '(' + co.co_varnames[oparg] + ')',
  114.             elif op in hascompare:
  115.                 print '(' + cmp_op[oparg] + ')',
  116.             elif op in hasfree:
  117.                 if free is None:
  118.                     free = co.co_cellvars + co.co_freevars
  119.                 print '(' + free[oparg] + ')',
  120.             
  121.         print 
  122.  
  123.  
  124. def disassemble_string(code, lasti = -1, varnames = None, names = None, constants = None):
  125.     labels = findlabels(code)
  126.     n = len(code)
  127.     i = 0
  128.     while i < n:
  129.         c = code[i]
  130.         op = ord(c)
  131.         if i == lasti:
  132.             print '-->',
  133.         else:
  134.             print '   ',
  135.         if i in labels:
  136.             print '>>',
  137.         else:
  138.             print '  ',
  139.         print repr(i).rjust(4), opname[op].ljust(15),
  140.         i = i + 1
  141.         if op >= HAVE_ARGUMENT:
  142.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  143.             i = i + 2
  144.             print repr(oparg).rjust(5),
  145.             if op in hasconst:
  146.                 if constants:
  147.                     print '(' + repr(constants[oparg]) + ')',
  148.                 else:
  149.                     print '(%d)' % oparg,
  150.             elif op in hasname:
  151.                 if names is not None:
  152.                     print '(' + names[oparg] + ')',
  153.                 else:
  154.                     print '(%d)' % oparg,
  155.             elif op in hasjrel:
  156.                 print '(to ' + repr(i + oparg) + ')',
  157.             elif op in haslocal:
  158.                 if varnames:
  159.                     print '(' + varnames[oparg] + ')',
  160.                 else:
  161.                     print '(%d)' % oparg,
  162.             elif op in hascompare:
  163.                 print '(' + cmp_op[oparg] + ')',
  164.             
  165.         print 
  166.  
  167. disco = disassemble
  168.  
  169. def findlabels(code):
  170.     '''Detect all offsets in a byte code which are jump targets.
  171.  
  172.     Return the list of offsets.
  173.  
  174.     '''
  175.     labels = []
  176.     n = len(code)
  177.     i = 0
  178.     while i < n:
  179.         c = code[i]
  180.         op = ord(c)
  181.         i = i + 1
  182.         if op >= HAVE_ARGUMENT:
  183.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  184.             i = i + 2
  185.             label = -1
  186.             if op in hasjrel:
  187.                 label = i + oparg
  188.             elif op in hasjabs:
  189.                 label = oparg
  190.             if label >= 0:
  191.                 if label not in labels:
  192.                     labels.append(label)
  193.                 
  194.             
  195.         return labels
  196.  
  197.  
  198. def findlinestarts(code):
  199.     '''Find the offsets in a byte code which are start of lines in the source.
  200.  
  201.     Generate pairs (offset, lineno) as described in Python/compile.c.
  202.  
  203.     '''
  204.     byte_increments = [ ord(c) for c in code.co_lnotab[0::2] ]
  205.     line_increments = [ ord(c) for c in code.co_lnotab[1::2] ]
  206.     lastlineno = None
  207.     lineno = code.co_firstlineno
  208.     addr = 0
  209.     for byte_incr, line_incr in zip(byte_increments, line_increments):
  210.         if byte_incr:
  211.             if lineno != lastlineno:
  212.                 yield (addr, lineno)
  213.                 lastlineno = lineno
  214.             addr += byte_incr
  215.         lineno += line_incr
  216.     
  217.     if lineno != lastlineno:
  218.         yield (addr, lineno)
  219.  
  220.  
  221. def _test():
  222.     '''Simple test program to disassemble a file.'''
  223.     if sys.argv[1:]:
  224.         if sys.argv[2:]:
  225.             sys.stderr.write('usage: python dis.py [-|file]\n')
  226.             sys.exit(2)
  227.         fn = sys.argv[1]
  228.         if not fn or fn == '-':
  229.             fn = None
  230.         
  231.     else:
  232.         fn = None
  233.     if fn is None:
  234.         f = sys.stdin
  235.     else:
  236.         f = open(fn)
  237.     source = f.read()
  238.     if fn is not None:
  239.         f.close()
  240.     else:
  241.         fn = '<stdin>'
  242.     code = compile(source, fn, 'exec')
  243.     dis(code)
  244.  
  245. if __name__ == '__main__':
  246.     _test()
  247.